<template>
  <el-dialog
    :title="type==='1'?'新增乐骑卫士用户信息':'修改乐骑卫士用户信息'"
    width="50%"
    :visible.sync="visible"
    @close="$emit('update:show', false)"
    :show="show"
  >
    <el-form  :model="formInline"  label-width="80px" label-position="left" :rules="rules" ref="updateUser">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="姓名" prop="userName">
            <el-input v-model="formInline.userName" placeholder="姓名"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="手机号" prop="telephone">
            <el-input v-model="formInline.telephone" placeholder="手机号" ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="车牌号" prop="number">
            <el-input v-model="formInline.number" placeholder="车牌号" :disabled="listen"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="品牌" prop="brand" >
            <el-input v-model="formInline.brand" placeholder="品牌"  :disabled="isClick"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="颜色" prop="color">
            <el-input v-model="formInline.color" placeholder="颜色"  :disabled="isClick"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12"  v-if="type==='1'">
          <el-form-item label="设备编号" prop="trackerId">
            <el-input v-model="formInline.trackerId" placeholder="设备编号" @focus="focus"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <choose-device v-on:chooseTracker="chooseTracker" :clear="show"  v-if="type==='1' && isFocus"></choose-device>
    </el-form>
    <div slot="footer" class="dialog-footer">
    <el-button @click="close('updateUser')">取 消</el-button>
    <el-button type="primary" @click="sure('updateUser')">确 定</el-button>
  </div>
  </el-dialog>
</template>

<script>
  import ShowMadal from '@/mixins/showmadal'
  import ChooseDevice from './chooseDevice'
  export default {
    name: 'Updateuser',
    mixins: [ShowMadal],
    props: {
      row: {
        type: Object,
        default: {}
      },
      type: {
        type: String,
        default: '1'
      }
    },
    computed:{
      listen:function () {
        this.isClick=true
        if(this.row.trackerId){
          this.isClick=false
        }

        return this.isClick
      }
    },
    watch: {
      row: function() {
        this.formInline = {
          userName: this.row.userName,
          telephone: this.row.telephone,
          number: this.row.number,
          brand: this.row.brand ,
          color: this.row.color ,
          trackerId:this.row.trackerId,
          userId:this.row.userId
        }
      },
      show(val){
        if(val){
          this.isClick=true
        }
      }
    },
    components:{
      ChooseDevice
    },
    data: function() {
      var checkPhone = (rule, value, callback) => {
        const telephone = /^(13[0-9]{9})|(18[0-9]{9})|(14[0-9]{9})|(17[0-9]{9})|(15[0-9]{9})|(19[0-9]{9})|(16[0-9]{9})$/
        if (!telephone.test(value)) {
          return callback(new Error('请填写正确的手机号'))
        }else {
          callback()
        }
      }

      var checkTrackerId = (rule, value, callback) => {
        if (!value) {
          this.isClick=true
          return callback()
        }else {
          this.isClick=false
          callback()
        }
      }
      return {
        formInline: {},
        choosed:"",
        isClick:true,
        isFocus:false,
        rules: {
          telephone: [
            { required: true, message: '请输入手机号', trigger: 'blur' },
            { type: 'number', validator: checkPhone, trigger: 'blur' }
          ],
          trackerId: [
            { validator: checkTrackerId, trigger: 'change' }
          ],
          userName: [
            { required: true, message: '请输入姓名', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      sure(formName) {
        this.$emit('submit', {formName: this.$refs[formName], formData: this.formInline})
      },
      close(formName) {
        this.$emit('close', this.$refs[formName])
      },
      chooseTracker(e){
        this.formInline.trackerId = e
        this.choosed = e
      },
      focus(){
        this.isFocus=true
      }
    }
  }
</script>

